# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.exchange.base.kr_rest.product import NativeProduct
from coin.exchange.huobi.kr_rest.currency import HuobiCurrency
from coin.exchange.huobi.kr_rest.constants import (
    symbol_list,
    quote_currency_list,
    currency_list,
)


class HuobiProduct(NativeProduct):
  _exchange = "Huobi"
  NativeCurrencyType = HuobiCurrency

  def __init__(self, base, quote):
    assert base != quote, (base, quote)
    native_symbol = "%s%s" % (base.native_symbol, quote.native_symbol)
    super().__init__(base, quote, native_symbol)

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert isinstance(product, str), type(product)
    assert product in symbol_list, product
    for quote in quote_currency_list:
      quote = HuobiCurrency.FromStrCurrency(quote)
      if product.endswith(quote.native_currency):
        try:
          base = HuobiCurrency.FromStrNativeCurrency(
              product[:-len(quote.native_currency)])
        except Exception as e:
          continue
        return HuobiProduct(base, quote)
    raise ValueError("Unable to parse: %s" % (product,))
